home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / mc220.zip / ROBOFACE.C < prev    next >
C/C++ Source or Header  |  1992-02-24  |  2KB  |  109 lines

  1. /*
  2.  * This function draws a very crude "robot" face on the PC video
  3.  * screen, using block characters on an 80x25 matrix. In addition
  4.  * to entertaining children, it shows some simple techniques for
  5.  * drawing lines, boxes and circles, which could be applied to
  6.  * higher resolution graphics modes.
  7.  */
  8.  
  9. #include \mc\stdio.h        /* Standard I/O definitions */
  10. #include \mc\video.h        /* Video    I/O definitions */
  11.  
  12. #define    PCHR    0xDB
  13.  
  14. /*
  15.  * Main program, draw the face
  16.  */
  17. main()
  18. {
  19.     int i;
  20.  
  21.     vopen();
  22.     vcursor_off();
  23.     box(1, 0, 77, 24);
  24.     for(i=1; i < 5; ++i) {
  25.         circle(17, 8, i);
  26.         circle(60, 8, i); }
  27.     circle(17, 8, 6);
  28.     circle(60, 8, 6);
  29.     line(38, 9, 42, 13);
  30.     line(38, 9, 34, 13);
  31.     line(35, 13, 41, 13);
  32.     box(24, 18, 53, 21);
  33.     vgetc();
  34.     vcursor_line();
  35.     vclscr();
  36. }
  37.  
  38. /*
  39.  * Draw a line from point (x1, y1) to (x2, y2)
  40.  */
  41. line(x1, y1, x2, y2)
  42.     int x1, y1, x2, y2;
  43. {
  44.     int i, w, h;
  45.  
  46.     /* If 'X' is greater, increment through 'X' coordinate */
  47.     if((w = abs(x1 - x2)) >= (h = abs(y1 - y2))) {
  48.         if(x1 > x2) {
  49.             i = x1;
  50.             x1 = x2;
  51.             x2 = i;
  52.             i = y1;
  53.             y1 = y2;
  54.             y2 = i; }
  55.         h = y2 - y1;
  56.  
  57.         for(i=0; i < w; ++i) {
  58.             vgotoxy(x1+i, y1+((i*h) / w));
  59.             vputc(PCHR); } }
  60.     /* If 'Y' is greater, increment through 'Y' coordinate */
  61.     else {
  62.         if(y1 > y2) {
  63.             i = x1;
  64.             x1 = x2;
  65.             x2 = i;
  66.             i = y1;
  67.             y1 = y2;
  68.             y2 = i; }
  69.         w = x2 - x1;
  70.         for(i=0; i < h; ++i) {
  71.             vgotoxy(x1+((i*w)/h), y1+i);
  72.             vputc(PCHR); } }
  73.  
  74.     vgotoxy(x2, y2);
  75.     vputc(PCHR);
  76. }
  77.  
  78. /*
  79.  * Draw a box with opposite corners (x1, y1) to (x2, y2)
  80.  */
  81. box(x1, y1, x2, y2)
  82.     int x1, y1, x2, y2;
  83. {
  84.     line(x1, y1, x2, y1);
  85.     line(x1, y1, x1, y2);
  86.     line(x2, y1, x2, y2);
  87.     line(x1, y2, x2, y2);
  88. }
  89.  
  90. /*
  91.  * Draw a circle about point (x, y) of radus (r)
  92.  */
  93. circle(x, y, r)
  94.     int x, y, r;
  95. {
  96.     int i, j, k, rs, lj;
  97.  
  98.     rs = (lj = r)*r;
  99.     for(i=0; i <= r; ++i) {
  100.         j = k = sqrt(rs - (i*i));
  101.         do {
  102.             vgotoxy(x+i, y+j); vputc(PCHR);
  103.             vgotoxy(x+i, y-j); vputc(PCHR);
  104.             vgotoxy(x-i, y+j); vputc(PCHR);
  105.             vgotoxy(x-i, y-j); vputc(PCHR); }
  106.         while(++j < lj);
  107.         lj = k; }
  108. }
  109.